//
//  NextSpecialViewController.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/4.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "NextSpecialViewController.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import "FitHeader.h"
#import <UIImageView+WebCache.h>
#import "CYStepsTableViewCell.h"
#import "CYStuffTableViewCell.h"
@interface NextSpecialViewController ()<UITableViewDataSource, UITableViewDelegate>

@property(nonatomic, strong)NSString *titlePhotoString;
@property(nonatomic, strong)NSString *IntroString;
@property(nonatomic, strong)UIImageView *photoImage;
@property(nonatomic, strong)UILabel *nameLabel;
@property(nonatomic, strong)UILabel *infoLabel;
@property(nonatomic, strong)NSMutableArray *OtherStuffArray;
@property(nonatomic, strong)NSMutableArray *StepsArray;
@property(nonatomic, strong)UITableView *tableView;

@end

@implementation NextSpecialViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = YES;
    [self getData];
}

-(void)getData
{
    _StepsArray = [NSMutableArray array];
    _OtherStuffArray = [NSMutableArray array];
    NSString *PostString = @"http://api.haodou.com/index.php?appid=4&appkey=573bbd2fbd1a6bac082ff4727d952ba3&appsign=e20017c6dd83ba375922d7cdbefbe5e6&channel=appstore&deviceid=0f607264fc6318a92b9e13c65db7cd3c%7C7C4F45AC-D834-40CC-8E57-1C73070C0250%7C28F136CD-C3C2-4542-AF9B-2BAD4F1067E6&format=json&loguid=&method=Info.getInfo&nonce=1446619040&sessionid=1446618930&signmethod=md5&timestamp=1446619040&uuid=8aa885128b339761cd4b5fc380d868d6&v=2&vc=43&vn=v5.4.0";
    NSString *bodyString = [NSString stringWithFormat:@"rid=%@&sign=&uid=&uuid=8aa885128b339761cd4b5fc380d868d6", _tempModel.RecipeId];
    [HTTPMethod getDataByString:PostString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        
        NSDictionary *dic = [data objectForKey:@"result"];
        NSDictionary *DataDic = [dic objectForKey:@"info"];
        
        _IntroString = [DataDic objectForKey:@"Intro"];
        _titlePhotoString = [DataDic objectForKey:@"Cover"];
        _OtherStuffArray = [DataDic objectForKey:@"OtherStuff"];
        _StepsArray = [DataDic objectForKey:@"Steps"];

        [self headerViews];
        [_tableView reloadData];
    }];
}

-(void)headerViews
{
    UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 300 * ht)];
    _photoImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 250 * ht)];
    _photoImage.backgroundColor = [UIColor grayColor];
    [_photoImage sd_setImageWithURL:[NSURL URLWithString:_titlePhotoString]];
    _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, _photoImage.frame.origin.y + _photoImage.frame.size.height, 355 * wt, 50 * ht)];
    _infoLabel.text = _tempModel.Title;
    _infoLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:18];
    [headerView addSubview:_photoImage];
    [headerView addSubview:_infoLabel];
    _tableView.tableHeaderView = headerView;
}

-(void)loadView
{
    [super loadView];
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableFooterView = [[UIView alloc]init];
    [self.view addSubview:_tableView];
}
//分区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//分区title
-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return @"食材";
    }
    return @"步骤";
}
//每个分区cell个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return _OtherStuffArray.count;
    }
    return _StepsArray.count;
}
//cell高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 45;
    }
    return 85;
}

//cell样式
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *identifier = @"cell";
        CYStuffTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[CYStuffTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (_OtherStuffArray.count != 0) {
            cell.nameLabel.text = [_OtherStuffArray[indexPath.row]valueForKey:@"name"];
            cell.numLabel.text = [_OtherStuffArray[indexPath.row]valueForKey:@"weight"];
        }
        return cell;
    }
    else
    {
        static NSString *identifier = @"NextCell";
        CYStepsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[CYStepsTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if (_StepsArray.count != 0) {
            [cell.photoImage sd_setImageWithURL:[NSURL URLWithString:[_StepsArray[indexPath.row]valueForKey:@"StepPhoto"]]];
            cell.numLabel.text = [NSString stringWithFormat:@"%ld", indexPath.row + 1];
            cell.titleLabel.text = [_StepsArray[indexPath.row]valueForKey:@"Intro"];
        }
        return cell;
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
